package com.data4.supplier.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data4.feignapi.common.constants.SupplierConstants;
import com.data4.feignapi.common.constants.UserConstants;
import com.data4.feignapi.common.tools.RegexUtils;
import com.data4.feignapi.config.JwtUtils;
import com.data4.feignapi.dto.PhonePwdLoginDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.dto.SupplierDTO;
import com.data4.feignapi.entity.Supplier;
import com.data4.feignapi.vo.SupplierVO;
import com.data4.supplier.mapper.SupplierMapper;
import com.data4.supplier.service.SupplierService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.impl.DefaultClaims;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Resource
    private JwtUtils jwtUtils;

    @Override
    public Result login(@NotNull PhonePwdLoginDTO pwdLoginDTO) {
        Supplier supplier = lambdaQuery()
                .eq(Supplier::getPhone, pwdLoginDTO.getPhone())
                .eq(Supplier::getPwd, pwdLoginDTO.getPwd())
                .select(Supplier::getSupplierId, Supplier::getBoosName)
                .one();
        if (BeanUtil.isEmpty(supplier)) {
            return Result.fail(UserConstants.LOGIN_FAIL);
        }
        SupplierVO supplierVO = BeanUtil.toBean(supplier, SupplierVO.class);
        String token = SupplierTWTToken(supplierVO);
        return Result.ok(token);
    }

    @Override
    public Result getSupplier(Integer current, Integer num) {
        List<Supplier> suppliers = lambdaQuery()
                .page(new Page<>(current, num))
                .getRecords();
        return Result.ok(suppliers);
    }

    @Override
    public Result register(@NotNull SupplierDTO supplierDTO) {
        if (exists(new LambdaQueryWrapper<Supplier>()
                .eq(Supplier::getPhone, supplierDTO.getPhone()))) {
            return Result.fail(UserConstants.PHONE_EXIST);
        }
        boolean isPhone = RegexUtils.isPhoneInvalid(supplierDTO.getPhone());
        boolean isPWD = RegexUtils.isPasswordInvalid(supplierDTO.getPwd());
        if (!isPhone) {
            return Result.fail(UserConstants.PHONE_NOT_TRUE);
        }
        if (!isPWD) {
            return Result.fail(UserConstants.PWD_NOT_TRUE);
        }
        Supplier supplier = BeanUtil.toBean(supplierDTO, Supplier.class);
        save(supplier);
        return Result.ok(SupplierConstants.REGISTER_SUCCESS);
    }

    @Override
    public SupplierVO getSupplierById(Integer supplierId) {
        Supplier supplier = lambdaQuery()
                .eq(Supplier::getSupplierId, supplierId)
                .select(Supplier::getSupplierId, Supplier::getBoosName,
                        Supplier::getPhone,Supplier::getIntroduction)
                .one();
        SupplierVO supplierVO = BeanUtil.toBean(supplier, SupplierVO.class);
        return supplierVO;
    }

    private String SupplierTWTToken(SupplierVO supplierVO) {
        //创造负载体
        Claims claims = new DefaultClaims();
        //放入用户信息
        claims.put(SupplierConstants.USER_TOKEN_PREFIX, supplierVO);
        //生成token
        String token = jwtUtils.generateToken(claims);
        return token;
    }
}
